﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GIMS.BLL;
using KPIS.GERP.GIMS.IBLL;
using KPIS.GERP.GIMS.MODEL;
using System.Data;
using System.IO;

namespace KPIS.GERP.GIMS.APP.APP_UI.Add_Form
{
    public partial class ICM_Item_Request : System.Web.UI.Page
    {
        Common.Main cn = new Common.Main();
        CommandControl cmdFac = new CommandControl();
        IBLLPrePurchaseRequest bll = new PrePurchaseRequestBLL();
        IBLLTree bllTree = new TreeBLL();
           IBLLMasterPurchaseMapping bllPurchaseMap = new MasterPurchaseMappingBLL();
        IBLLMasterPurchaseRule bllRule = new MasterPurchaseRuleBLL();
        IBLLBgmProjectOperation bllOperation = new BgmProjectOperationBLL();
        IBLLBgmSource bllSource = new BgmSourceBLL();
        IBLLResourceMasterItem bllItem = new ResourceMasterItemBLL();
        IBLLBgmProjectExpense bllProExpense = new BgmProjectExpenseBLL();
        IBLLMasterCommitteeFunction bllComFunction = new MasterCommitteeFunctionBLL();
        IBLLMasterCommitteePosition bllComPosition = new MasterCommitteePositionBLL();
        IBLLMasterCommitteeMapping bllComMapping = new MasterCommitteeMappingBLL();
        IBLLHrmEmployee bllEmployee = new HrmEmployeeBLL();
        IBLLSysDurationCount bllDurationCount = new SysDurationCountBLL();
        IBLLSysDurationType bllDurationType = new SysDurationTypeBLL();
        static IList<PrePurchaseRequestItemInfo> infoListPprItem = new List<PrePurchaseRequestItemInfo>();
        static IList<PrePurchaseRequestItemInfo> infoListAddPprItem = new List<PrePurchaseRequestItemInfo>();
        static IList<PrePurchaseRequestCommitteeInfo> infoListCommittee = new List<PrePurchaseRequestCommitteeInfo>();
        static IList<PrePurchaseRequestCommitteeInfo> infoListAddCommittee = new List<PrePurchaseRequestCommitteeInfo>();
        static IList<DocumentInfo> infoListFile = new List<DocumentInfo>();
        static string stRule = "";
        double sumValue = 0;
        double totalPrice = 0;
        int nCount = 0;
        private const int ItemsPerRequest = 10;
        static double sumTotalAmount = 0;
        IBLLSysYearType bllYearType = new SysYearTypeBLL();
        string YearType = "";
        string Year = "";
        string PurchaseType = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //lblHeaderYearType.Text = Session["YearTypeDescr"].ToString();
                //lblHeaderYearTypeSeq.Text = Session["YearTypeSeq"].ToString();
                //lblHeaderBgmYear.Text = Session["Year"].ToString();
                //infoListPprItem.Clear();
                //LoadBgmExpenseProject();
                //LoadRootNodes(TreeItem, TreeNodeExpandMode.ServerSideCallBack);
                //LoadGridItem();
                //LoadGridCommittee();
                //LoadCommitteeFunction();
                //LoadCommitteePosition();
                //LoadGridPastCommittee();
                //LoadGridFile();
                //LoadGridSubCommittee();
                //LoadDurationType();
                //LoadDurationCount();
                //LoadJobSiteTree(); // Amp Method

                hdInvTypeSeq.Value = Request.QueryString["inv_t"].ToString() ;
                hdInvTypeName.Value = Request.QueryString["inv_n"].ToString() ;

                CreateItemRequestNo();
                LoadWareHouse();


               
                //          panelDateReq.Attributes.Add("style", "visibility:hidden");
                //panelDateReqPick.Attributes.Add("style", "visibility:hidden");
                //rdbDuration.Attributes.Add("onclick", "return ChangeDurationRequire('" + panelDuration.ClientID + "','" + panelDateReq.ClientID + "','" + panelDateReqPick.ClientID + "')");
                //rdbDate.Attributes.Add("onclick", "return ChangeDateRequire('" + panelDuration.ClientID + "','" + panelDateReq.ClientID + "','" + panelDateReqPick.ClientID + "')");
                if (Request.QueryString.Count > 0)
                {
               //     YearType = Session["yearSeq"].ToString();
                //    cbYearType.SelectedValue = YearType;

                 //   Year = Session["year"].ToString();
                 //   txtYear.Text = Year;
                    //lblPurchaseType.Text = Request.QueryString["mmn"].ToString();
                    //PurchaseType = Request.QueryString["mmt"].ToString();
                //    Session["PurchaseType"] = PurchaseType;
                }
            }
            else
            {
             //   PurchaseType = Session["PurchaseType"].ToString();
            }
            //panelPprMethod.Visible = cbPprType.SelectedIndex >= 0;
            BasicDatePicker1.DisplayType = BasicFrame.WebControls.DatePickerDisplayType.Image;
         //   BasicDatePicker2.DisplayType = BasicFrame.WebControls.DatePickerDisplayType.Image;

        }

        private void CreateItemRequestNo()
        {
            string ir = "IR";
            string y, m;
            y = DateTime.Today.Year.ToString();
            m = DateTime.Today.Month.ToString();
            if (m.Length == 1)
            {
                m = "0" + m;
            }
            RequestBLL obj = new RequestBLL();
            string run_no;
            run_no = obj.getIR_RefNO(m);
            int intTmp;
            intTmp = int.Parse(run_no) + 1;
            run_no = intTmp.ToString("0000"); 
            lblIR_RunNo.Text = ir + y + m + run_no;   


        }


        private static string GetStatusMessage(int offset, int total)
        {
            if (total <= 0)
                return "ไม่มีข้อมูล";

            return String.Format("รายการที่ <b>1</b>-<b>{0}</b> จากทั้งหมด <b>{1}</b> รายการ", offset, total);
        }


        private void LoadWareHouse()
        {
            RequestBLL obj = new RequestBLL();
            DataTable dt;
            dt = obj.SelectWareHouse();
            cbWareHouse.DataSource = dt;
            cbWareHouse.DataBind();  

        }

        private void InputItem(string seq, string name, string unit_name)
        {
            BgmExpenseInfo infoExpense = new BgmExpenseInfo();
            BgmSourceInfo infoSource = new BgmSourceInfo();
            ResourceMasterItemInfo infoItem = new ResourceMasterItemInfo();
            MasterUnitInfo infoUnit = new MasterUnitInfo();
            PrePurchaseRequestItemInfo infoPprItem = new PrePurchaseRequestItemInfo();
            MasterPurchaseRuleInfo infoRule = new MasterPurchaseRuleInfo();
            BgmProjectOperationExpenseInfo infoProOperExp = new BgmProjectOperationExpenseInfo();
            BgmProjectOperationInfo infoProOper = new BgmProjectOperationInfo();
            string[] all_value = seq.Split(new string[] { "-" }, StringSplitOptions.None);
            string[] all_name = name.Split(new string[] { " - " }, StringSplitOptions.None);
            foreach (PrePurchaseRequestItemInfo info in infoListAddPprItem)
            {
                if (info.InfoItem.Seq == Convert.ToInt32(all_value[3]))
                {
                    return;
                }
            }
            infoPprItem.Name = all_name[1];
            infoItem.Seq = Convert.ToInt32(all_value[3]);
            infoUnit.Name = unit_name;

                     infoProOper.Seq = Convert.ToInt32(hdOperationSeq.Value);
            infoProOper.Name = hdOperationName.Value;
         
            infoItem.InfoUnit = infoUnit;
            infoPprItem.InfoItem = infoItem;
            infoPprItem.InfoSource = infoSource;
            infoPprItem.InfoExpense = infoExpense;
            infoProOperExp.Seq = Convert.ToInt32(hdOperationExpSeq.Value.ToString());
        //    infoProOperExp.InfoOperation = infoProOper;
          //  infoPprItem.InfoOperationExpense = infoProOperExp;
            infoPprItem.InfoRule = infoRule;
            infoListAddPprItem.Add(infoPprItem);
        }

        //protected void LoadPurchaseType()
        //{
        ////    IList<MasterPurchaseTypeInfo> infoList = bllPurchaseType.GetMasterPurchaseType();
        //    cbPurchaseType.DataSource = infoList;
        //    cbPurchaseType.DataBind();
        //}

      

        //protected void LoadPprPriority()
        //{
        ////    IList<SysPprPriorityInfo> infoList = bllPprPriority.GetSysPprPriority();
        //    cbPriority.DataSource = infoList;
        //    cbPriority.DataBind();
        //}

        //protected void LoadPprType()
        //{
        ////    IList<SysPprTypeInfo> infoList = bllPprType.GetSysPprType();
        //    cbPprType.DataSource = infoList;
        //    cbPprType.DataBind();
        //}

        //protected void LoadPprMethod()
        //{
        ////    IList<SysPprMethodInfo> infoList = bllPprMethod.GetSysPprMethod();
        //    cbPprMethod.DataSource = infoList;
        //    cbPprMethod.DataBind();
        //}

  
       

        protected void TreeItem_NodeExpand(object sender, RadTreeNodeEventArgs e)
        {
            PopulateNodeOnDemand(e, TreeNodeExpandMode.ServerSideCallBack);
        }

        private static void LoadRootNodes(RadTreeView treeView, TreeNodeExpandMode expandMode)
        {
            IBLLTree bll = new TreeBLL();

            foreach (TreeInfo info in bll.GetResourceMasterTreeWithoutHeader(""))
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info.Name;
                node.ToolTip = info.Descr;
                node.Value = info.Id;
                node.ImageUrl = info.Image_url;
                node.Checkable = false;
                if (Convert.ToInt32(info.Children_count) > 0)
                {
                    node.ExpandMode = expandMode;
                }
                treeView.Nodes.Add(node);
            }
        }

        private static void PopulateNodeOnDemand(RadTreeNodeEventArgs e, TreeNodeExpandMode expandMode)
        {
            e.Node.Dispose();
            e.Node.Nodes.Clear();
            IBLLTree bll = new TreeBLL();
            foreach (TreeInfo info in bll.GetResourceMasterTreeWithoutHeader(e.Node.Value))
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info.Name;
                node.ToolTip = info.Descr;
                node.Value = info.Id;
                node.ImageUrl = info.Image_url;
                node.Checkable = (info.Category == "ITEM");
                if (Convert.ToInt32(info.Children_count) > 0)
                {
                    node.ExpandMode = expandMode;
                }
                e.Node.Nodes.Add(node);
            }

            e.Node.Expanded = true;
            e.Node.ExpandMode = expandMode;
        }

        protected void cbPprType_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            //RequiredFieldValidator valPprMethod = new RequiredFieldValidator();
            //valPprMethod.ID = "ValcbPprMethod";
            //valPprMethod.ControlToValidate = "cbPprMethod";
            //valPprMethod.SetFocusOnError = true;
            //valPprMethod.Display = ValidatorDisplay.Dynamic;
            //valPprMethod.ErrorMessage = "* กรุณาระบุวิธีการใช้จ่าย *";
            //panelValcbPprMethod.Controls.Add(valPprMethod);
            //ผ่านกระบวนการพัสดุมาตรฐาน
            //if (cbPprType.SelectedValue == "1")
            //{
            //    //ใช้วิธีการใช้จ่ายได้วิธีเดียวคือผ่านแผนการใช้จ่ายงบประมาณ
            //    cbPprMethod.SelectedIndex = 0;
            //    cbPprMethod.Enabled = false;
            //    panelMoneyRequest.Visible = false;
            //}
            ////ผ่านหน่วยงานจัดหาเอง
            //else if (cbPprType.SelectedValue == "2")
            //{
            //    cbPprMethod.Enabled = true;
            //}
        }

        protected void cbPprMethod_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            //ผ่านแผนการใช้จ่ายงบประมาณ
            //if (cbPprMethod.SelectedValue == "1")
            //{
            //    panelMoneyRequest.Visible = false;
            //}
            ////ผ่านใบยืม
            //else if (cbPprMethod.SelectedValue == "2")
            //{
            //    panelMoneyRequest.Visible = true;
            //    RequiredFieldValidator valMoneyRequest = new RequiredFieldValidator();
            //    valMoneyRequest.ID = "ValcbMoneyRequest";
            //    valMoneyRequest.ControlToValidate = "cbMoneyRequest";
            //    valMoneyRequest.SetFocusOnError = true;
            //    valMoneyRequest.Display = ValidatorDisplay.Dynamic;
            //    valMoneyRequest.ErrorMessage = "* กรุณาระบุใบยืม *";
            //    panelValcbMoneyRequest.Controls.Add(valMoneyRequest);
            //}
        }




        protected void nextButton_Detail_Click(object sender, EventArgs e)
        {
            RadManager1.ResponseScripts.Add(@"ChangeTab('Item',1);");
        }

        protected void backButton_Item_Click(object sender, EventArgs e)
        {
            RadManager1.ResponseScripts.Add(@"ChangeTab('Detail',0);");
        }

        protected void nextButton_Item_Click(object sender, EventArgs e)
        {
            RadManager1.ResponseScripts.Add(@"ChangeTab('Committee',2);");
        }

        protected void backButton_Committee_Click(object sender, EventArgs e)
        {
            RadManager1.ResponseScripts.Add(@"ChangeTab('Item',1);");
        }

        protected void nextButton_Committee_Click(object sender, EventArgs e)
        {
            RadManager1.ResponseScripts.Add(@"ChangeTab('File',3);");
        }

        protected void backButton_File_Click(object sender, EventArgs e)
        {
            RadManager1.ResponseScripts.Add(@"ChangeTab('Committee',2);");
        }

        protected void finishButton_File_Click(object sender, EventArgs e)
        {
            PrePurchaseRequestInfo info = new PrePurchaseRequestInfo();
            SysYearTypeInfo infoYearType = new SysYearTypeInfo();
            HrmJobSiteInfo infoJobSiteRequest = new HrmJobSiteInfo();
            HrmJobSiteInfo infoJobSite = new HrmJobSiteInfo();
            HrmJobSiteInfo infoJobSiteDirect = new HrmJobSiteInfo();
            HrmEmployeeInfo infoEmployee = new HrmEmployeeInfo();
            MasterPurchaseTypeInfo infoPurchaseType = new MasterPurchaseTypeInfo();
            //SysPprPriorityInfo infoPriority = new SysPprPriorityInfo();
            SysPprForInfo infoFor = new SysPprForInfo();
            SysPprTypeInfo infoPprType = new SysPprTypeInfo();
            //SysPprMethodInfo infoPprMethod = new SysPprMethodInfo();
            //FimMoneyRequestInfo infoMoneyRequest = new FimMoneyRequestInfo();
            SysDurationTypeInfo infoDurationType = new SysDurationTypeInfo();
            SysDurationCountInfo infoDurationCount = new SysDurationCountInfo();
            RecordInfo infoRecord = new RecordInfo();
            string lastNumber = "";

            //infoYearType.Seq = Convert.ToInt32(lblHeaderYearTypeSeq.Text);
            //info.Year = Convert.ToInt32(lblHeaderBgmYear.Text);

            //ต้องรอแก้ไข
          //  RadTreeView JobSitetree = (RadTreeView)this.cbJobSite.Items[0].FindControl("TreeViewJobSite");
         //   string[] seq = JobSitetree.SelectedValue.ToString().Split('-');
            //infoJobSiteRequest.Seq = Convert.ToInt32(seq[1].ToString());
            //infoJobSite.Seq = Convert.ToInt32(seq[1].ToString());
            //infoJobSiteDirect.Seq = Convert.ToInt32(seq[1].ToString());

            infoEmployee.Seq = 1;
           // info.DateRequested = Convert.ToDateTime(txtDate.TextWithLiterals);
            infoPurchaseType.Seq = Convert.ToInt32(this.PurchaseType);
            info.Name = txtName.Text;

            //if (cbPriority.Text.Length > 0)
            //{
                //infoPriority.Seq = Convert.ToInt32(cbPriority.SelectedValue);
            //}
          
            infoPprType.Seq = 1; //จัดหาผ่านกระบวนการพัสดุมาตรฐาน
            //infoPprMethod.Seq = Convert.ToInt32(cbPprMethod.SelectedValue);
            //if (panelMoneyRequest.Visible)
            //{
            //    infoMoneyRequest.Seq = Convert.ToInt32(cbMoneyRequest.SelectedValue);
            //}
       
         

            infoRecord.Created_by = cn.UserID;
            infoRecord.Created_when = DateTime.Now;
            info.InfoEmployeeRequest = infoEmployee;
            info.InfoPurchaseType = infoPurchaseType;
            info.InfoDurationCount = infoDurationCount;
            info.InfoDurationType = infoDurationType;
            info.InfoJobSite = infoJobSite;
            info.InfoJobSiteDirect = infoJobSiteDirect;
            info.InfoJobSiteRequest = infoJobSiteRequest;
            //info.InfoMoneyRequest = infoMoneyRequest;
            info.InfoPprFor = infoFor;
            //info.InfoPprMethod = infoPprMethod;
            //info.InfoPprPriority = infoPriority;
            info.InfoPprType = infoPprType;
            info.InfoYearType = infoYearType;
            info.InfoRecord = infoRecord;

            try
            {
                lastNumber = bll.Insert(info, infoListPprItem, infoListCommittee, infoListFile);
            }
            catch
            {
                RadManager1.Alert(SystemInfo._InsertError);
                return;
            }

            RadManager1.Alert("เลขที่รายการของท่านคือ : '" + lastNumber + "'");
            infoListPprItem.Clear();
            infoListCommittee.Clear();
            infoListFile.Clear();
            RadManager1.ResponseScripts.Add(@"CloseWindow();");
        }

        protected void RadManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            string[] all_value = e.Argument.Split(new string[] { " : " }, StringSplitOptions.None);
            if (all_value[0] == "ExpensePlan")
            {
                infoListAddPprItem.Clear();
                string[] data_value = all_value[1].Split(new string[] { "-" }, StringSplitOptions.None);
             //   hdRule.Value = data_value[0];
                hdOperationSeq.Value = data_value[2];
                hdOperationExpSeq.Value = data_value[3];
                BgmProjectExpenseInfo infoProExpense = bllProExpense.GetBgmProjectExpense(Convert.ToInt32(data_value[3]));
                MasterPurchaseRuleInfo infoRule = bllRule.GetMasterPurchaseRule(Convert.ToInt32(data_value[0]));
                BgmProjectOperationInfo infoOperation = bllOperation.GetBgmProjectOperation(Convert.ToInt32(data_value[2]));
  
                BgmProjectOperationExpenseInfo infoProOperExp = new BgmProjectOperationExpenseInfo();
                if (infoProExpense.InfoItem.Seq != null) //เป็นค่าครุภัณฑ์ ที่ดิน สิ่งก่อสร้าง
                {
                    
                    BgmSourceInfo infoSource = new BgmSourceInfo();
                    BgmExpenseInfo infoExpense = new BgmExpenseInfo();
                    ResourceMasterItemInfo infoItem = new ResourceMasterItemInfo();
                    MasterUnitInfo infoUnit = new MasterUnitInfo();
                    PrePurchaseRequestItemInfo infoPprItem = new PrePurchaseRequestItemInfo();
                    infoSource.Seq = infoProExpense.InfoSource.Seq;
                    infoSource.Name = infoProExpense.InfoSource.Name;
                    infoPprItem.Name = infoProExpense.InfoItem.Name;
                    infoPprItem.Qty = infoProExpense.Qty;
                    infoPprItem.Price = infoProExpense.Price;
                    infoExpense.Seq = infoProExpense.InfoExpense.Seq;
                    infoExpense.Name = infoProExpense.InfoExpense.Name;
                    infoItem.Seq = infoProExpense.InfoItem.Seq;
                    infoUnit.Name = infoProExpense.InfoItem.InfoUnit.Name;
                    infoItem.InfoUnit = infoUnit;
                    infoPprItem.InfoItem = infoItem;
                    infoPprItem.InfoExpense = infoExpense;
                    infoPprItem.InfoSource = infoSource;
                    infoPprItem.InfoRule = infoRule;

                    infoProOperExp.Seq = Convert.ToInt32(hdOperationExpSeq.Value.ToString());
        //           infoProOperExp.InfoOperation = infoOperation;
               //     infoPprItem.InfoOperationExpense = infoProOperExp;

                    infoListAddPprItem.Add(infoPprItem);
                }
                else if (infoProExpense.InfoExpense.Name.IndexOf("วัสดุ") > 0) // เป็นค่าวัสดุ
                {
                    //hdSourceSeq.Value = infoProExpense.InfoSource.Seq.ToString();
                    //hdSourceName.Value = infoProExpense.InfoSource.Name;
                    //hdExpenseSeq.Value = infoProExpense.InfoExpense.Seq.ToString();
                    //hdExpenseName.Value = infoProExpense.InfoExpense.Name;
                    //hdRule.Value = infoRule.Seq.ToString();
                    //hdRuleName.Value = infoRule.Name;
                    hdOperationSeq.Value = infoOperation.Seq.ToString();
                    hdOperationName.Value = infoOperation.Name;
                
                }
                else //เป็นรายจ่ายอื่นๆ เช่น ค่าจ้างเหมาบริการ 
                {
               
                    BgmSourceInfo infoSource = new BgmSourceInfo();
                    BgmExpenseInfo infoExpense = new BgmExpenseInfo();
                    ResourceMasterItemInfo infoItem = new ResourceMasterItemInfo();
                    MasterUnitInfo infoUnit = new MasterUnitInfo();
                    PrePurchaseRequestItemInfo infoPprItem = new PrePurchaseRequestItemInfo();
                    infoSource.Seq = infoProExpense.InfoSource.Seq;
                    infoSource.Name = infoProExpense.InfoSource.Name;
                    infoPprItem.Name = infoProExpense.InfoExpense.Name;
                    infoPprItem.Qty = infoProExpense.Qty;
                    infoPprItem.Price = infoProExpense.Price;
                    infoExpense.Seq = infoProExpense.InfoExpense.Seq;
                    infoExpense.Name = infoProExpense.InfoExpense.Name;
                    infoUnit.Name = "รายการ";
                    infoItem.InfoUnit = infoUnit;
                    infoPprItem.InfoItem = infoItem;
                    infoPprItem.InfoExpense = infoExpense;
                    infoPprItem.InfoSource = infoSource;
                    infoPprItem.InfoRule = infoRule;

                    infoProOperExp.Seq = Convert.ToInt32(hdOperationExpSeq.Value.ToString());
                    //infoProOperExp.InfoOperation = infoOperation;
                    //infoPprItem.InfoOperationExpense = infoProOperExp;

                    infoListAddPprItem.Add(infoPprItem);
                }

  

            }
        }

  

                
        protected void OnAjaxUpdate(object sender, ToolTipUpdateEventArgs args)
        {
            this.UpdateToolTip(args.Value, args.UpdatePanel);
        }

        private void UpdateToolTip(string elementID, UpdatePanel panel)
        {
            string[] all_value = elementID.Split(new string[] { ":" }, StringSplitOptions.None);
            if (all_value[0] == "Item")
            {
                Session["Resource_Item_Id"] = all_value[1];
                Session["Expense_Id"] = null;
            }
            else
            {
                Session["Resource_Item_Id"] = null;
                Session["Expense_Id"] = all_value[1];
            }
            Control ctrl = Page.LoadControl("../UserControl/ICM_Pre_Purchase_Request.ascx");
            panel.ContentTemplateContainer.Controls.Add(ctrl);
        }

        protected void OnAjaxUpdate2(object sender, ToolTipUpdateEventArgs args)
        {
            this.UpdateToolTip2(args.Value, args.UpdatePanel);
        }

        private void UpdateToolTip2(string elementID, UpdatePanel panel)
        {
            string[] all_value = elementID.Split(new string[] { ":" }, StringSplitOptions.None);
            //if (all_value[0] == "Item")
            //{
            //    Session["Resource_Item_Id"] = all_value[1];
            //    Session["Expense_Id"] = null;
            //}
            //else
            //{
            //    Session["Resource_Item_Id"] = null;
            //    Session["Expense_Id"] = all_value[1];
            //}
            Session["Resource_Item_Id"] = all_value[1];
            Control ctrl = Page.LoadControl("../UserControl/TIP_Item_Detail.ascx");
            panel.ContentTemplateContainer.Controls.Add(ctrl);
        }




        protected void cbGridInternalName_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
        {
            IList<HrmEmployeeInfo> infoList = bllEmployee.GetHrmEmployee(e.Text);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, infoList.Count);
            e.EndOfItems = (endOffset == infoList.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                ((RadComboBox)sender).Items.Add(new RadComboBoxItem(infoList[i].Name, infoList[i].Seq.ToString()));
                TableRow tr = new TableRow();
                TableCell tdJobSite = new TableCell();
                Label lblJobSite = new Label();
                lblJobSite.Text = infoList[i].InfoJobSite.Name;
                tdJobSite.Width = Unit.Pixel(100);
                tdJobSite.Controls.Add(lblJobSite);
                tr.Cells.Add(tdJobSite);
                TableCell tdName = new TableCell();
                Label lblName = new Label();
                lblName.Text = infoList[i].Name;
                tdName.Width = Unit.Pixel(100);
                tdName.Controls.Add(lblName);
                tr.Cells.Add(tdName);
                TableCell tdPosition = new TableCell();
                Label lblPosition = new Label();
                lblPosition.Text = infoList[i].InfoPosition.Name;
                tdPosition.Width = Unit.Pixel(100);
                tdPosition.Controls.Add(lblPosition);
                tr.Cells.Add(tdPosition);
                TableCell tdClass = new TableCell();
                Label lblClass = new Label();
                lblClass.Text = infoList[i].InfoClass.Name;
                tdClass.Width = Unit.Pixel(100);
                tdClass.Controls.Add(lblClass);
                tr.Cells.Add(tdClass);
                ((Table)((RadComboBox)sender).Items[j].FindControl("tableGridInternalName")).Controls.Add(tr);
                j++;
            }

            e.Message = GetStatusMessage(endOffset, infoList.Count);
        }


        protected void Button1_Click(object sender, EventArgs e)
        {

            string icm_req_code, request_name, reason;
            DateTime wanted_date, request_date;
            int from_icm_wh_seq, icm_res_type_seq;

            RequestBLL obj = new RequestBLL();


            icm_res_type_seq = int.Parse(hdInvTypeSeq.Value);   
            from_icm_wh_seq = int.Parse(cbWareHouse.SelectedValue);  
            icm_req_code = lblIR_RunNo.Text;  
            request_name = txtName.Text;   
            reason = txtReson.Text;
            string str_wanted_date, str_request_date;

            str_wanted_date = Utils.Date.ToEngFormatDate(txtDateWanted.TextWithLiterals);
            str_request_date = Utils.Date.ToEngFormatDate(txtDate.TextWithLiterals);
            wanted_date = Convert.ToDateTime(str_wanted_date);
            request_date = Convert.ToDateTime(str_request_date);
            

            obj.Insert(icm_req_code, request_name, wanted_date, request_date, reason, from_icm_wh_seq, icm_res_type_seq);

            RadManager1.ResponseScripts.Add(@"CloseWindow();");
        }

        protected void cbNewOperationName_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
        {

            IList<HrmEmployeeInfo> infoList = bllEmployee.GetHrmEmployee(e.Text);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = (endOffset == infoList.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                RadComboBox cbAppEmp = (RadComboBox)sender;
                cbAppEmp.Items.Add(new RadComboBoxItem(infoList[i].Name, infoList[i].Seq.ToString()));
                TableRow tr = new TableRow();
                TableCell tdJobSite = new TableCell();
                Label lblJobSite = new Label();
                lblJobSite.Text = infoList[i].InfoJobSite.Name;
                tdJobSite.Width = Unit.Pixel(200);
                tdJobSite.Controls.Add(lblJobSite);
                tr.Cells.Add(tdJobSite);
                TableCell tdName = new TableCell();
                Label lblName = new Label();
                lblName.Text = infoList[i].Name;
                tdName.Width = Unit.Pixel(150);
                tdName.Controls.Add(lblName);
                tr.Cells.Add(tdName);
                TableCell tdPosition = new TableCell();
                Label lblPosition = new Label();
                lblPosition.Text = infoList[i].InfoPosition.Name;
                tdPosition.Width = Unit.Pixel(150);
                tdPosition.Controls.Add(lblPosition);
                tr.Cells.Add(tdPosition);
                TableCell tdClass = new TableCell();
                Label lblClass = new Label();
                lblClass.Text = infoList[i].InfoClass.Name;
                tdClass.Width = Unit.Pixel(190);
                tdClass.Controls.Add(lblClass);
                tr.Cells.Add(tdClass);
                ((Table)cbAppEmp.Items[j].FindControl("tableNewOperationName")).Controls.Add(tr);
                j++;
            }

            e.Message = GetStatusMessage(endOffset, infoList.Count);





        }

        protected void BasicDatePicker2_SelectionChanged(object sender, EventArgs e)
        {

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            string test;
            test = cbNewOperationName.SelectedValue.ToString();




        }
    }
}
